﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;

public partial class Page_view : System.Web.UI.Page
{
    public CMS.Functions fn = new CMS.Functions();
    public StoredProcedureValues Results = new StoredProcedureValues();

    private long? AdmID = 0;
    private int Action;

    protected void Page_Init(object sender, EventArgs e)
    {

        if (!CMS.Functions.IsMasterUser()) { Response.Redirect("Page.aspx"); }
        
        if (!CMS.Functions.VerifyLogin.Valid) { Response.Redirect("Login.aspx"); }
        else
        {
            if (Request.QueryString["AdmID"] != null) { AdmID = Convert.ToInt64(Request.QueryString["AdmID"].ToString()); }
            if (Request.QueryString["CMD"] != null) { Action = Convert.ToInt16(Request.QueryString["CMD"].ToString()); }

            Session.Add("AdmID", AdmID);
            Session.Add("CMD", Action);

            if (!IsPostBack) { FillValues(); }
        }
    }

    void FillValues()
    {
        if (Action == CMD.View || Action == CMD.Update || Action == CMD.Delete)
        {
            var Adm = CMS.Administrations.GetAdministration(AdmID, null, null, null, null,null).FirstOrDefault();

            if (Adm != null && AdmID.HasValue)
            {
                ADM_CODE.Text = Adm.ADM_CODE;
                ADM_NAME.Text = Adm.ADM_NAME;
                AdmTitle.Text = " - " + Adm.ADM_NAME;
                ADM_COMMENTS.Text = Adm.ADM_COMMENTS;
                ADM_ACTIVE.Checked = Adm.ADM_ACTIVE.Value;
            }
        }

        if (Action == CMD.Delete)
        {
            DisableControls.DisableControl(this.Form);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Action == CMD.Update)
        {
            BtnSave.Text = "Edit";
        }
        else if (Action == CMD.Delete)
        {
            BtnSave.Text = "Delete";
        }

    }

    void Values(Int64 Action)
    {
        if (Action == CMD.Insert)
        {

            Results = CMS.StoredProcedureValues.SP_Administration(CMD.Insert, AdmID, ADM_CODE.Text, ADM_NAME.Text, ADM_ACTIVE.Checked, ADM_COMMENTS.Text, DateTime.Now);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Inserted succesfully !", "Administration.aspx");
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }
        }

        if (Action == CMD.Update)
        {
            Results = CMS.StoredProcedureValues.SP_Administration(CMD.Update, AdmID, ADM_CODE.Text, ADM_NAME.Text, ADM_ACTIVE.Checked, ADM_COMMENTS.Text, null);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Updated succesfully !", "Administration.aspx");
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }

        }

        if (Action == CMD.Delete)
        {
            Results = CMS.StoredProcedureValues.SP_Administration(CMD.Delete, AdmID, null, null, null, null,null);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Deleted succesfully !", "Administration.aspx");
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message,System.Drawing.KnownColor.Red);
            }

        }

    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Administration.aspx");
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Action != CMD.Delete)
        {

            if (!String.IsNullOrEmpty(ADM_CODE.Text) && !String.IsNullOrEmpty(ADM_NAME.Text))
            {
                Values(Action);
            }
            else
            {
                ((Content)Master).Jscript =
                String.IsNullOrEmpty(ADM_CODE.Text) ? CMS.Functions.NormalPopup("Please enter Code !",
                System.Drawing.KnownColor.Black,
                " $('.Code').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") :
                String.IsNullOrEmpty(ADM_NAME.Text) ? CMS.Functions.NormalPopup("Please enter Name !",
                System.Drawing.KnownColor.Black,
                " $('.Name').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") : null;
            }
        }
        else
        {
           Values(Action);
        }
    }
}